<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程用户">
        <el-select
          v-model="queryParams.user"
          size="small"
          style="width: 240px" @change="handleUserChange">
          <el-option
            v-for="item in userOptions"
            :key="item.user"
            :label="item.userName"
            :value="item.user">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="清单编号" prop="boqCode">
        <el-input
          v-model="queryParams.boqCode"
          placeholder="请输入清单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery(true)">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openSelectMeterage"
          v-if="this.editFlag"
          v-hasPermi="['engineering:change:detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          v-if="this.editFlag"
          @click="handleUpdate"
          v-hasPermi="['engineering:change:detail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-if="this.editFlag"
          v-hasPermi="['engineering:change:detail:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="清单编号" align="left" prop="boqCode" width="100" />
      <el-table-column label="清单名称" align="left" prop="boqName" width="150" show-overflow-tooltip  />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="数量" align="center" prop="amount" />
      <el-table-column label="金额" align="center" prop="total" />
      <el-table-column label="起始桩号" align="center" prop="beginStation" />
      <el-table-column label="截止桩号" align="center" prop="endStation" />
      <el-table-column label="分部" align="center" prop="place" />
      <el-table-column label="分项" align="center" prop="item" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="editFlag"
            v-hasPermi="['engineering:change:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="editFlag"
            v-hasPermi="['engineering:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <select-meterage ref="select" :changeId="queryParams.changeId" @ok="handleQuery(false)"></select-meterage>

    <!-- 添加或修改变更令申报详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="清单编码" prop="boqCode">
          <el-input v-model="form.boqCode" disabled />
        </el-form-item>
        <el-form-item label="清单名称" prop="boqName">
          <el-input v-model="form.boqName" disabled />
        </el-form-item>
        <el-form-item label="单　　位" prop="unit">
          <el-input v-model="form.unit" disabled />
        </el-form-item>
        <el-form-item label="单　　价" prop="price">
          <el-input v-model="form.price" disabled />
        </el-form-item>
        <el-form-item label="数　　量" prop="amount">
          <el-input v-model="form.amount" type="number" @input="calTotal" @keydown.native="handleKey" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="金　　额" prop="total">
          <el-input v-model="form.total" type="number" @keydown.native="handleKey" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="起始桩号" prop="beginStation">
          <el-input v-model="form.beginStation"/>
        </el-form-item>
        <el-form-item label="截止桩号" prop="endStation">
          <el-input v-model="form.endStation"/>
        </el-form-item>
        <el-form-item label="分　　部" prop="place">
          <el-input v-model="form.place" disabled/>
        </el-form-item>
        <el-form-item label="分　　项" prop="item">
          <el-input v-model="form.item" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail, selectUserOptions } from "@/api/engineering/changeDetail";
import selectMeterage from "./selectMeterage";
import * as math from 'mathjs';
import { getDecimalPlace } from "@/api/engineering/change";

export default {
  name: "Detail",
  components: { selectMeterage },
  data() {
    return {
      // 不能编辑的原因
      // 1.该数据未锁定
      // 2.该数据的当前用户不等于登录用户
      // 3.选择用户不等于登录用户
      editFlag: true,
      // 主表状态
      status: null,
      // 当前登录用户
      loginUser: null,
      // 当前流程所在用户
      currentUser: null,
      // 选择用户
      selectUserName: null,
      // 用户下拉框
      userOptions: [],
      // 小数点位数
      decimalPlace: 0,
      // 是否可以超计量
      ultraMeasure: "N",
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 变更令申报详情表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        changeId: null,
        user: null,
        boqCode: null,
        boqName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        amount: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        total: [
          { required: true, message: "金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.loginUser = this.$store.state.user.name;
    this.queryParams.changeId = this.$route.query.changeId;
    this.status = this.$route.query.status;
    console.log(this.status);
    this.selectUserOptions();
    this.getDecimalPlace();
  },
  methods: {
    /** 获取用户节点列表 */
    selectUserOptions() {
      selectUserOptions(this.queryParams.changeId).then(response => {
        this.currentUser = response.currentUser;
        if (this.loginUser != this.currentUser || this.status == '1') {
          this.editFlag = false;
        } else {
          this.editFlag = true;
        }
        this.queryParams.user = response.currentUser;
        this.userOptions = response.nodes;
        this.getList();
      });
    },
    /** 获取小数点位数 */
    getDecimalPlace() {
      getDecimalPlace(this.queryParams.changeId).then(response => {
        this.decimalPlace = response.decimalPlace;
        this.ultraMeasure = response.ultraMeasure;
        console.log(this.decimalPlace);
        console.log(this.ultraMeasure);
      });
    },
    /** 查询变更令申报详情列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        changeDetailId: null,
        changeId: null,
        meterageBoqCode: null,
        boqCode: null,
        boqName: null,
        ancestors: null,
        orderNum: null,
        unit: null,
        price: null,
        amount: null,
        total: null,
        place: null,
        item: null,
        beginStation: null,
        endStation: null,
        dept: null,
        deptName: null,
        user: null,
        userName: null
      };
      this.resetForm("form");
    },
    // 用户下拉改变触发事件
    handleUserChange(val) {
      if (this.loginUser == this.currentUser && this.status == "0" && this.loginUser == val) {
        this.editFlag = true;
      } else {
        this.editFlag = false;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery(flag) {
      if (flag) {
        this.queryParams.pageNum = 1;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery(true);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.changeDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 打开选择计量清单窗口 */
    openSelectMeterage() {
      this.$refs.select.show();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const changeDetailId = row.changeDetailId || this.ids
      getDetail(changeDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改变更清单";
      });
    },
    // 计算金额
    calTotal() {
      this.form.total = math.round(this.form.amount * this.form.price, this.decimalPlace);
    },
    // 禁止输入e
    handleKey(e) {
      let key = e.key
      // 不允许输入'e'
      if (key === 'e') {
        e.returnValue = false
        return false
      }
      return true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.changeDetailId != null) {
            updateDetail(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const changeDetailIds = row.changeDetailId || this.ids;
      this.$modal.confirm('确认删除选中的数据项？').then(function() {
        return delDetail(changeDetailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    }
  }
};
</script>

<style lang="scss">
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type='number'] {
    -moz-appearance: textfield;
  }
</style>
